﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.EntityModifiers;
using Spinning.Spinning.Level;
using Microsoft.Xna.Framework.Input.Touch;

namespace Spinning.Spinning.Entity.Scenes
{
    class TeachWindScene : TeachScene
    {
        List<Sprite> _sprites2 = new List<Sprite>();
        List<List<Sprite>> _spriteListList = new List<List<Sprite>>();
        Sprite spriteText1;

        public override void OnLoad()
        {
            base.OnLoad();
            Sprite sprite1 = new Sprite();
            sprite1.textureName = "teachWind/wind_1";
            sprite1.ZIndex = Constants.background;
            sprite1.Alpha = 1;
            sprite1.OnLoad();
            _sprites.Add(sprite1);
            this.Attach(sprite1);

            Sprite sprite2 = new Sprite();
            sprite2.textureName = "teachWind/wind_2";
            sprite2.ZIndex = Constants.background;
            sprite2.Alpha = 0;
            sprite2.OnLoad();
            _sprites.Add(sprite2);
            this.Attach(sprite2);

            Sprite sprite3 = new Sprite();
            sprite3.textureName = "teachWind/wind_3";
            sprite3.ZIndex = Constants.background;
            sprite3.Alpha = 0;
            sprite3.OnLoad();
            _sprites.Add(sprite3);
            this.Attach(sprite3);

            Sprite sprite4 = new Sprite();
            sprite4.textureName = "teachWind/wind_4";
            sprite4.ZIndex = Constants.background;
            sprite4.Alpha = 0;
            sprite4.OnLoad();
            _sprites.Add(sprite4);
            this.Attach(sprite4);

            Sprite sprite5 = new Sprite();
            sprite5.textureName = "teachWind/wind_5";
            sprite5.ZIndex = Constants.background;
            sprite5.Alpha = 0;
            sprite5.OnLoad();
            _sprites2.Add(sprite5);
            this.Attach(sprite5);

            Sprite sprite6 = new Sprite();
            sprite6.textureName = "teachWind/wind_6";
            sprite6.ZIndex = Constants.background;
            sprite6.Alpha = 0;
            sprite6.OnLoad();
            _sprites2.Add(sprite6);
            this.Attach(sprite6);

            spriteText = new Sprite();
            spriteText.textureName = "teachWind/wind_text_1";
            spriteText.ZIndex = Constants.ui;
            spriteText.Alpha = 0;
            spriteText.Position = new Vector2(240 - 170, 182);
            spriteText.OnLoad();
            this.Attach(spriteText);

            spriteText1 = new Sprite();
            spriteText1.textureName = "teachWind/wind_text_2";
            spriteText1.ZIndex = Constants.ui;
            spriteText1.Alpha = 0;
            spriteText1.Position = new Vector2(240 - 122, 182);
            spriteText1.OnLoad();
            this.Attach(spriteText1);

            _spriteListList.Add(_sprites);
            _spriteListList.Add(_sprites2);
        }

        public override void OnStartFunction()
        {
            Game1.stageManager.GoToScene("wind");
        }

        int _gameTime, flag1;

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            int delta = gameTime.ElapsedGameTime.Milliseconds;
            _gameTime += delta;
            if (_gameTime / 1000 >= 1)
            {
                flag1 = -flag1 + 1;
                _gameTime = _gameTime % 1000;
            }
            if (flag == 0)
            {
                spriteText1.Alpha = 0;
                if (flag1 == 0) spriteText.Alpha = ((float)(_gameTime % 1000) / 1000) * 0.7f + 0.3f;
                else spriteText.Alpha = (1f - (float)(_gameTime % 1000) / 1000) * 0.7f + 0.3f;
            }
            else 
            {
                spriteText.Alpha = 0;
                if (flag1 == 0) spriteText1.Alpha = ((float)(_gameTime % 1000) / 1000) * 0.7f + 0.3f;
                else spriteText1.Alpha = (1f - (float)(_gameTime % 1000) / 1000) * 0.7f + 0.3f;
            }
        }


        int pre ,current,flag,counter;
        int[] number = new int[] { 2, 1 };
        //3 2

        protected override void DoChangeSprite()
        {
            _spriteListList[flag][pre].Alpha = 0;
            current++;
            if (current == _spriteListList[flag].Count) //一輪
            {
                current = 0;
                counter++;
                if (counter == number[flag]) 
                {
                    flag = 1 - flag;
                    counter = 0;
                }
            }
            _spriteListList[flag][current].Alpha = 1;
            pre = current;
        }

        public override Scene Clone()
        {
            Scene scene = new TeachWindScene();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();
            return scene;
        }
    }
}
